package org.groupg.project.a;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import java.text.SimpleDateFormat;
import java.time.LocalDate;

public class JsonProcessor {
    private static final ObjectMapper jsonMapper = new ObjectMapper();

    static {
        // JSON 配置
        jsonMapper.enable(SerializationFeature.INDENT_OUTPUT);
        jsonMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        jsonMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        jsonMapper.disable(com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

        // 注册模块
        jsonMapper.registerModule(new JavaTimeModule());

        // 自定义序列化器模块
        SimpleModule customModule = new SimpleModule();
        customModule.addSerializer(LocalDate.class, new CustomDateSerializer());
        customModule.addDeserializer(LocalDate.class, new CustomDateDeserializer());
        jsonMapper.registerModule(customModule);
    }

    public static String toJson(Employee employee) throws JsonProcessingException {
        return jsonMapper.writeValueAsString(employee);
    }

    public static Employee fromJson(String json) throws JsonProcessingException {
        return jsonMapper.readValue(json, Employee.class);
    }
}